package wangmin.message.mgr_web.web.controller;

import com.dap.dao.model.Page;
import com.sbp.message.api.entity.MessageStatus;
import com.sbp.message.api.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wangmin.message.mgr_web.web.entity.CommonResponseBody;
import com.sbp.message.api.entity.MessageVO;
import wangmin.message.mgr_web.web.entity.type.CommonResponseCode;
import wangmin.message.mgr_web.web.service.AuthService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>User: Wang Min
 * <p>Date: 2016-12-20
 * <p>Version: 1.0
 */
@RestController
@RequestMapping("/message")
@Api(description = "数据项的相关操作")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private AuthService authService;


	@RequestMapping(value = "/page", method = RequestMethod.GET, produces = "application/json")
    @ApiOperation(value = "消息分页查询")
    public Page<MessageVO> page(
            @RequestParam("messageStatus") MessageStatus messageStatus,
            @RequestParam("orderBy") String orderByFieldName,
            @RequestParam("desc") boolean desc,
            @RequestParam("offset") int offset,
            @RequestParam("limit") int limit,
            HttpServletRequest request) {
        if (!authService.hasLogin(request))
            return null;


        Map<String, Object> parameter = new HashMap<>();
        parameter.put("status", messageStatus.getValue());
        parameter.put("orderByField", orderByFieldName);
        if (desc) {
            parameter.put("desc", desc);
        }

        return messageService.queryMessageVOPage(parameter, 1 + offset / limit, limit);
    }

    @RequestMapping(value = "/reliveAndSendMessage/{messageId}", method = RequestMethod.POST, produces = "application/json")
    @ApiOperation(value = "复活发送消息状态")
    public CommonResponseBody reliveAndSendMessage(@PathVariable("messageId") String messageId, HttpServletRequest request) {
        if (!authService.hasLogin(request))
            return new CommonResponseBody(CommonResponseCode.permissionError, "请登录");


        if (messageService.reliveAndSendMessage(messageId))
            return new CommonResponseBody();
        else
            return new CommonResponseBody(CommonResponseCode.paramError, "messageId["+messageId+"], not exist");
    }
}
